package mypackage.dao;

import java.util.Vector;

import net.rim.device.api.collection.util.SortedReadableList;
import net.rim.device.api.ui.component.KeywordProvider;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;

public class ItemDataProvider extends SortedReadableList implements
		KeywordProvider {

	public ItemDataProvider(Vector items) {
		super(new Comparator() {
			public int compare(Object o1, Object o2) {
				boolean b1 = ((ListItemDao) o1).done, b2 = ((ListItemDao) o2).done;
				if (!b1 && b2)
					return -1;
				else if (b1 && !b2)
					return 1;
				else
					return 0;
			}
		});
		initFromVector(items);
	}

	public void deleteItem(Object element) {
		doRemove(element);
	}

	public void updateItem(Object oldO, Object newO) {
		doUpdate(oldO, newO);
	}

	public void addItem(String str) {
		doAdd(new ListItemDao(str, null, false));
	}

	public String[] getKeywords(Object element) {
		return StringUtilities.stringToWords(element.toString());
	}

	public void deleteAll() {
		for (int i = size()-1; i >= 0; i--)
			{
				doRemove(getAt(i));
				
			}
	}

	public void delete(boolean marked) {
		for (int i = size()-1; i >= 0; i--) {
			ListItemDao listItemDao = (ListItemDao) getAt(i);
			if (listItemDao.done == marked)
				doRemove(listItemDao);
		}
	}

	public Vector getVector() {
		Vector vector = new Vector();
		for (int i = 0; i < size(); i++)
			vector.addElement(getAt(i));
		return vector;
	}

	public void initFromVector(Vector vector) {

		for (int i = 0; i < size(); i++)
			doRemove(getAt(i));
		for (int i = 0; i < vector.size(); i++)
			doAdd(vector.elementAt(i));
	}

}
